﻿#region Using directives

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

using VCS.Entities;
using VCS.Data;

#endregion

namespace VCS.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="MedicalProcedureProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract partial class MedicalProcedureProviderBaseCore : EntityProviderBase<VCS.Entities.MedicalProcedure, VCS.Entities.MedicalProcedureKey>
	{		
		#region Get from Many To Many Relationship Functions
		#region GetByMemberTypeFromProcedureDiscount
		
		/// <summary>
		///		Gets MedicalProcedure objects from the datasource by MemberType in the
		///		ProcedureDiscount table. Table MedicalProcedure is related to table MemberType
		///		through the (M:N) relationship defined in the ProcedureDiscount table.
		/// </summary>
		/// <param name="_memberType"></param>
		/// <returns>Returns a typed collection of MedicalProcedure objects.</returns>
		public TList<MedicalProcedure> GetByMemberTypeFromProcedureDiscount(System.String _memberType)
		{
			int count = -1;
			return GetByMemberTypeFromProcedureDiscount(null,_memberType, 0, int.MaxValue, out count);
			
		}
		
		/// <summary>
		///		Gets VCS.Entities.MedicalProcedure objects from the datasource by MemberType in the
		///		ProcedureDiscount table. Table MedicalProcedure is related to table MemberType
		///		through the (M:N) relationship defined in the ProcedureDiscount table.
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_memberType"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of MedicalProcedure objects.</returns>
		public TList<MedicalProcedure> GetByMemberTypeFromProcedureDiscount(System.String _memberType, int start, int pageLength)
		{
			int count = -1;
			return GetByMemberTypeFromProcedureDiscount(null, _memberType, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets MedicalProcedure objects from the datasource by MemberType in the
		///		ProcedureDiscount table. Table MedicalProcedure is related to table MemberType
		///		through the (M:N) relationship defined in the ProcedureDiscount table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_memberType"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of MedicalProcedure objects.</returns>
		public TList<MedicalProcedure> GetByMemberTypeFromProcedureDiscount(TransactionManager transactionManager, System.String _memberType)
		{
			int count = -1;
			return GetByMemberTypeFromProcedureDiscount(transactionManager, _memberType, 0, int.MaxValue, out count);
		}
		
		
		/// <summary>
		///		Gets MedicalProcedure objects from the datasource by MemberType in the
		///		ProcedureDiscount table. Table MedicalProcedure is related to table MemberType
		///		through the (M:N) relationship defined in the ProcedureDiscount table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_memberType"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of MedicalProcedure objects.</returns>
		public TList<MedicalProcedure> GetByMemberTypeFromProcedureDiscount(TransactionManager transactionManager, System.String _memberType,int start, int pageLength)
		{
			int count = -1;
			return GetByMemberTypeFromProcedureDiscount(transactionManager, _memberType, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets MedicalProcedure objects from the datasource by MemberType in the
		///		ProcedureDiscount table. Table MedicalProcedure is related to table MemberType
		///		through the (M:N) relationship defined in the ProcedureDiscount table.
		/// </summary>
		/// <param name="_memberType"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of MedicalProcedure objects.</returns>
		public TList<MedicalProcedure> GetByMemberTypeFromProcedureDiscount(System.String _memberType,int start, int pageLength, out int count)
		{
			
			return GetByMemberTypeFromProcedureDiscount(null, _memberType, start, pageLength, out count);
		}


		/// <summary>
		///		Gets MedicalProcedure objects from the datasource by MemberType in the
		///		ProcedureDiscount table. Table MedicalProcedure is related to table MemberType
		///		through the (M:N) relationship defined in the ProcedureDiscount table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <param name="_memberType"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of MedicalProcedure objects.</returns>
		public abstract TList<MedicalProcedure> GetByMemberTypeFromProcedureDiscount(TransactionManager transactionManager,System.String _memberType, int start, int pageLength, out int count);
		
		#endregion GetByMemberTypeFromProcedureDiscount
		
		#endregion	
		
		#region Delete Methods

		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to delete.</param>
		/// <returns>Returns true if operation suceeded.</returns>
		public override bool Delete(TransactionManager transactionManager, VCS.Entities.MedicalProcedureKey key)
		{
			return Delete(transactionManager, key.ProcedureCode);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="_procedureCode">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public bool Delete(System.String _procedureCode)
		{
			return Delete(null, _procedureCode);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_procedureCode">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public abstract bool Delete(TransactionManager transactionManager, System.String _procedureCode);		
		
		#endregion Delete Methods
		
		#region Get By Foreign Key Functions
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Procedure_ProcedureSubCategory key.
		///		FK_Procedure_ProcedureSubCategory Description: 
		/// </summary>
		/// <param name="_subCategoryId"></param>
		/// <returns>Returns a typed collection of VCS.Entities.MedicalProcedure objects.</returns>
		public TList<MedicalProcedure> GetBySubCategoryId(System.String _subCategoryId)
		{
			int count = -1;
			return GetBySubCategoryId(_subCategoryId, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Procedure_ProcedureSubCategory key.
		///		FK_Procedure_ProcedureSubCategory Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_subCategoryId"></param>
		/// <returns>Returns a typed collection of VCS.Entities.MedicalProcedure objects.</returns>
		/// <remarks></remarks>
		public TList<MedicalProcedure> GetBySubCategoryId(TransactionManager transactionManager, System.String _subCategoryId)
		{
			int count = -1;
			return GetBySubCategoryId(transactionManager, _subCategoryId, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_Procedure_ProcedureSubCategory key.
		///		FK_Procedure_ProcedureSubCategory Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_subCategoryId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of VCS.Entities.MedicalProcedure objects.</returns>
		public TList<MedicalProcedure> GetBySubCategoryId(TransactionManager transactionManager, System.String _subCategoryId, int start, int pageLength)
		{
			int count = -1;
			return GetBySubCategoryId(transactionManager, _subCategoryId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Procedure_ProcedureSubCategory key.
		///		fkProcedureProcedureSubCategory Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_subCategoryId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of VCS.Entities.MedicalProcedure objects.</returns>
		public TList<MedicalProcedure> GetBySubCategoryId(System.String _subCategoryId, int start, int pageLength)
		{
			int count =  -1;
			return GetBySubCategoryId(null, _subCategoryId, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Procedure_ProcedureSubCategory key.
		///		fkProcedureProcedureSubCategory Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_subCategoryId"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of VCS.Entities.MedicalProcedure objects.</returns>
		public TList<MedicalProcedure> GetBySubCategoryId(System.String _subCategoryId, int start, int pageLength,out int count)
		{
			return GetBySubCategoryId(null, _subCategoryId, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Procedure_ProcedureSubCategory key.
		///		FK_Procedure_ProcedureSubCategory Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_subCategoryId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of VCS.Entities.MedicalProcedure objects.</returns>
		public abstract TList<MedicalProcedure> GetBySubCategoryId(TransactionManager transactionManager, System.String _subCategoryId, int start, int pageLength, out int count);
		
		#endregion

		#region Get By Index Functions
		
		/// <summary>
		/// 	Gets a row from the DataSource based on its primary key.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to retrieve.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <returns>Returns an instance of the Entity class.</returns>
		public override VCS.Entities.MedicalProcedure Get(TransactionManager transactionManager, VCS.Entities.MedicalProcedureKey key, int start, int pageLength)
		{
			return GetByProcedureCode(transactionManager, key.ProcedureCode, start, pageLength);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key PK_Procedure index.
		/// </summary>
		/// <param name="_procedureCode"></param>
		/// <returns>Returns an instance of the <see cref="VCS.Entities.MedicalProcedure"/> class.</returns>
		public VCS.Entities.MedicalProcedure GetByProcedureCode(System.String _procedureCode)
		{
			int count = -1;
			return GetByProcedureCode(null,_procedureCode, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_Procedure index.
		/// </summary>
		/// <param name="_procedureCode"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="VCS.Entities.MedicalProcedure"/> class.</returns>
		public VCS.Entities.MedicalProcedure GetByProcedureCode(System.String _procedureCode, int start, int pageLength)
		{
			int count = -1;
			return GetByProcedureCode(null, _procedureCode, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_Procedure index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_procedureCode"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="VCS.Entities.MedicalProcedure"/> class.</returns>
		public VCS.Entities.MedicalProcedure GetByProcedureCode(TransactionManager transactionManager, System.String _procedureCode)
		{
			int count = -1;
			return GetByProcedureCode(transactionManager, _procedureCode, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_Procedure index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_procedureCode"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="VCS.Entities.MedicalProcedure"/> class.</returns>
		public VCS.Entities.MedicalProcedure GetByProcedureCode(TransactionManager transactionManager, System.String _procedureCode, int start, int pageLength)
		{
			int count = -1;
			return GetByProcedureCode(transactionManager, _procedureCode, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_Procedure index.
		/// </summary>
		/// <param name="_procedureCode"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="VCS.Entities.MedicalProcedure"/> class.</returns>
		public VCS.Entities.MedicalProcedure GetByProcedureCode(System.String _procedureCode, int start, int pageLength, out int count)
		{
			return GetByProcedureCode(null, _procedureCode, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_Procedure index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_procedureCode"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="VCS.Entities.MedicalProcedure"/> class.</returns>
		public abstract VCS.Entities.MedicalProcedure GetByProcedureCode(TransactionManager transactionManager, System.String _procedureCode, int start, int pageLength, out int count);
						
		#endregion "Get By Index Functions"
	
		#region Custom Methods
		
		
		#endregion

		#region Helper Functions	
		
		/// <summary>
		/// Fill a TList&lt;MedicalProcedure&gt; From a DataReader.
		/// </summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">number of rows.</param>
		/// <returns>a <see cref="TList&lt;MedicalProcedure&gt;"/></returns>
		public static TList<MedicalProcedure> Fill(IDataReader reader, TList<MedicalProcedure> rows, int start, int pageLength)
		{
			NetTiersProvider currentProvider = DataRepository.Provider;
            bool useEntityFactory = currentProvider.UseEntityFactory;
            bool enableEntityTracking = currentProvider.EnableEntityTracking;
            LoadPolicy currentLoadPolicy = currentProvider.CurrentLoadPolicy;
			Type entityCreationFactoryType = currentProvider.EntityCreationalFactoryType;
			
			// advance to the starting row
			for (int i = 0; i < start; i++)
			{
				if (!reader.Read())
				return rows; // not enough rows, just return
			}
			for (int i = 0; i < pageLength; i++)
			{
				if (!reader.Read())
					break; // we are done
					
				string key = null;
				
				VCS.Entities.MedicalProcedure c = null;
				if (useEntityFactory)
				{
					key = new System.Text.StringBuilder("MedicalProcedure")
					.Append("|").Append((System.String)reader[((int)MedicalProcedureColumn.ProcedureCode - 1)]).ToString();
					c = EntityManager.LocateOrCreate<MedicalProcedure>(
					key.ToString(), // EntityTrackingKey
					"MedicalProcedure",  //Creational Type
					entityCreationFactoryType,  //Factory used to create entity
					enableEntityTracking); // Track this entity?
				}
				else
				{
					c = new VCS.Entities.MedicalProcedure();
				}
				
				if (!enableEntityTracking ||
					c.EntityState == EntityState.Added ||
					(enableEntityTracking &&
					
						(
							(currentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
							(currentLoadPolicy == LoadPolicy.DiscardChanges && c.EntityState != EntityState.Unchanged)
						)
					))
				{
					c.SuppressEntityEvents = true;
					c.ProcedureCode = (System.String)reader[((int)MedicalProcedureColumn.ProcedureCode - 1)];
					c.OriginalProcedureCode = c.ProcedureCode;
					c.ProcedureDescription = (reader.IsDBNull(((int)MedicalProcedureColumn.ProcedureDescription - 1)))?null:(System.String)reader[((int)MedicalProcedureColumn.ProcedureDescription - 1)];
					c.DrCode = (System.String)reader[((int)MedicalProcedureColumn.DrCode - 1)];
					c.SubCategoryId = (reader.IsDBNull(((int)MedicalProcedureColumn.SubCategoryId - 1)))?null:(System.String)reader[((int)MedicalProcedureColumn.SubCategoryId - 1)];
					c.UnitPrice = (System.Decimal)reader[((int)MedicalProcedureColumn.UnitPrice - 1)];
					c.AllowCustom = (System.Boolean)reader[((int)MedicalProcedureColumn.AllowCustom - 1)];
					c.Remark = (reader.IsDBNull(((int)MedicalProcedureColumn.Remark - 1)))?null:(System.String)reader[((int)MedicalProcedureColumn.Remark - 1)];
					c.UpdateDate = (System.DateTime)reader[((int)MedicalProcedureColumn.UpdateDate - 1)];
					c.CreateUser = (reader.IsDBNull(((int)MedicalProcedureColumn.CreateUser - 1)))?null:(System.String)reader[((int)MedicalProcedureColumn.CreateUser - 1)];
					c.CreateDate = (System.DateTime)reader[((int)MedicalProcedureColumn.CreateDate - 1)];
					c.UpdateUser = (reader.IsDBNull(((int)MedicalProcedureColumn.UpdateUser - 1)))?null:(System.String)reader[((int)MedicalProcedureColumn.UpdateUser - 1)];
					c.EntityTrackingKey = key;
					c.AcceptChanges();
					c.SuppressEntityEvents = false;
				}
				rows.Add(c);
			}
		return rows;
		}		
		/// <summary>
		/// Refreshes the <see cref="VCS.Entities.MedicalProcedure"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="VCS.Entities.MedicalProcedure"/> object to refresh.</param>
		public static void RefreshEntity(IDataReader reader, VCS.Entities.MedicalProcedure entity)
		{
			if (!reader.Read()) return;
			
			entity.ProcedureCode = (System.String)reader[((int)MedicalProcedureColumn.ProcedureCode - 1)];
			entity.OriginalProcedureCode = (System.String)reader["ProcedureCode"];
			entity.ProcedureDescription = (reader.IsDBNull(((int)MedicalProcedureColumn.ProcedureDescription - 1)))?null:(System.String)reader[((int)MedicalProcedureColumn.ProcedureDescription - 1)];
			entity.DrCode = (System.String)reader[((int)MedicalProcedureColumn.DrCode - 1)];
			entity.SubCategoryId = (reader.IsDBNull(((int)MedicalProcedureColumn.SubCategoryId - 1)))?null:(System.String)reader[((int)MedicalProcedureColumn.SubCategoryId - 1)];
			entity.UnitPrice = (System.Decimal)reader[((int)MedicalProcedureColumn.UnitPrice - 1)];
			entity.AllowCustom = (System.Boolean)reader[((int)MedicalProcedureColumn.AllowCustom - 1)];
			entity.Remark = (reader.IsDBNull(((int)MedicalProcedureColumn.Remark - 1)))?null:(System.String)reader[((int)MedicalProcedureColumn.Remark - 1)];
			entity.UpdateDate = (System.DateTime)reader[((int)MedicalProcedureColumn.UpdateDate - 1)];
			entity.CreateUser = (reader.IsDBNull(((int)MedicalProcedureColumn.CreateUser - 1)))?null:(System.String)reader[((int)MedicalProcedureColumn.CreateUser - 1)];
			entity.CreateDate = (System.DateTime)reader[((int)MedicalProcedureColumn.CreateDate - 1)];
			entity.UpdateUser = (reader.IsDBNull(((int)MedicalProcedureColumn.UpdateUser - 1)))?null:(System.String)reader[((int)MedicalProcedureColumn.UpdateUser - 1)];
			entity.AcceptChanges();
		}
		
		/// <summary>
		/// Refreshes the <see cref="VCS.Entities.MedicalProcedure"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="VCS.Entities.MedicalProcedure"/> object.</param>
		public static void RefreshEntity(DataSet dataSet, VCS.Entities.MedicalProcedure entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.ProcedureCode = (System.String)dataRow["ProcedureCode"];
			entity.OriginalProcedureCode = (System.String)dataRow["ProcedureCode"];
			entity.ProcedureDescription = Convert.IsDBNull(dataRow["ProcedureDescription"]) ? null : (System.String)dataRow["ProcedureDescription"];
			entity.DrCode = (System.String)dataRow["DrCode"];
			entity.SubCategoryId = Convert.IsDBNull(dataRow["SubCategoryID"]) ? null : (System.String)dataRow["SubCategoryID"];
			entity.UnitPrice = (System.Decimal)dataRow["UnitPrice"];
			entity.AllowCustom = (System.Boolean)dataRow["AllowCustom"];
			entity.Remark = Convert.IsDBNull(dataRow["Remark"]) ? null : (System.String)dataRow["Remark"];
			entity.UpdateDate = (System.DateTime)dataRow["UpdateDate"];
			entity.CreateUser = Convert.IsDBNull(dataRow["CreateUser"]) ? null : (System.String)dataRow["CreateUser"];
			entity.CreateDate = (System.DateTime)dataRow["CreateDate"];
			entity.UpdateUser = Convert.IsDBNull(dataRow["UpdateUser"]) ? null : (System.String)dataRow["UpdateUser"];
			entity.AcceptChanges();
		}
		#endregion 
		
		#region DeepLoad Methods
		/// <summary>
		/// Deep Loads the <see cref="IEntity"/> object with criteria based of the child 
		/// property collections only N Levels Deep based on the <see cref="DeepLoadType"/>.
		/// </summary>
		/// <remarks>
		/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire object graph.
		/// </remarks>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="entity">The <see cref="VCS.Entities.MedicalProcedure"/> object to load.</param>
		/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
		/// <param name="deepLoadType">DeepLoadType Enumeration to Include/Exclude object property collections from Load.</param>
		/// <param name="childTypes">VCS.Entities.MedicalProcedure Property Collection Type Array To Include or Exclude from Load</param>
		/// <param name="innerList">A collection of child types for easy access.</param>
	    /// <exception cref="ArgumentNullException">entity or childTypes is null.</exception>
	    /// <exception cref="ArgumentException">deepLoadType has invalid value.</exception>
		public override void DeepLoad(TransactionManager transactionManager, VCS.Entities.MedicalProcedure entity, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes, DeepSession innerList)
		{
			if(entity == null)
				return;

			#region SubCategoryIdSource	
			if (CanDeepLoad(entity, "ProcedureSubCategory|SubCategoryIdSource", deepLoadType, innerList) 
				&& entity.SubCategoryIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = (entity.SubCategoryId ?? string.Empty);
				ProcedureSubCategory tmpEntity = EntityManager.LocateEntity<ProcedureSubCategory>(EntityLocator.ConstructKeyFromPkItems(typeof(ProcedureSubCategory), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.SubCategoryIdSource = tmpEntity;
				else
					entity.SubCategoryIdSource = DataRepository.ProcedureSubCategoryProvider.GetBySubCategoryId(transactionManager, (entity.SubCategoryId ?? string.Empty));		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'SubCategoryIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.SubCategoryIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.ProcedureSubCategoryProvider.DeepLoad(transactionManager, entity.SubCategoryIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion SubCategoryIdSource
			
			//used to hold DeepLoad method delegates and fire after all the local children have been loaded.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			// Deep load child collections  - Call GetByProcedureCode methods when available
			
			#region InvoiceDetailsCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<InvoiceDetails>|InvoiceDetailsCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'InvoiceDetailsCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.InvoiceDetailsCollection = DataRepository.InvoiceDetailsProvider.GetByProcedureCode(transactionManager, entity.ProcedureCode);

				if (deep && entity.InvoiceDetailsCollection.Count > 0)
				{
					deepHandles.Add("InvoiceDetailsCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<InvoiceDetails>) DataRepository.InvoiceDetailsProvider.DeepLoad,
						new object[] { transactionManager, entity.InvoiceDetailsCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			#region ProcedureDiscountCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<ProcedureDiscount>|ProcedureDiscountCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'ProcedureDiscountCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.ProcedureDiscountCollection = DataRepository.ProcedureDiscountProvider.GetByProcedureCode(transactionManager, entity.ProcedureCode);

				if (deep && entity.ProcedureDiscountCollection.Count > 0)
				{
					deepHandles.Add("ProcedureDiscountCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<ProcedureDiscount>) DataRepository.ProcedureDiscountProvider.DeepLoad,
						new object[] { transactionManager, entity.ProcedureDiscountCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			#region MemberTypeMemberTypeCollection_From_ProcedureDiscount
			// RelationshipType.ManyToMany
			if (CanDeepLoad(entity, "List<MemberType>|MemberTypeMemberTypeCollection_From_ProcedureDiscount", deepLoadType, innerList))
			{
				entity.MemberTypeMemberTypeCollection_From_ProcedureDiscount = DataRepository.MemberTypeProvider.GetByProcedureCodeFromProcedureDiscount(transactionManager, entity.ProcedureCode);			 
		
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'MemberTypeMemberTypeCollection_From_ProcedureDiscount' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.MemberTypeMemberTypeCollection_From_ProcedureDiscount != null)
				{
					deepHandles.Add("MemberTypeMemberTypeCollection_From_ProcedureDiscount",
						new KeyValuePair<Delegate, object>((DeepLoadHandle< MemberType >) DataRepository.MemberTypeProvider.DeepLoad,
						new object[] { transactionManager, entity.MemberTypeMemberTypeCollection_From_ProcedureDiscount, deep, deepLoadType, childTypes, innerList }
					));
				}
			}
			#endregion
			
			
			
			//Fire all DeepLoad Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			deepHandles = null;
		}
		
		#endregion 
		
		#region DeepSave Methods

		/// <summary>
		/// Deep Save the entire object graph of the VCS.Entities.MedicalProcedure object with criteria based of the child 
		/// Type property array and DeepSaveType.
		/// </summary>
		/// <param name="transactionManager">The transaction manager.</param>
		/// <param name="entity">VCS.Entities.MedicalProcedure instance</param>
		/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.</param>
		/// <param name="childTypes">VCS.Entities.MedicalProcedure Property Collection Type Array To Include or Exclude from Save</param>
		/// <param name="innerList">A Hashtable of child types for easy access.</param>
		public override bool DeepSave(TransactionManager transactionManager, VCS.Entities.MedicalProcedure entity, DeepSaveType deepSaveType, System.Type[] childTypes, DeepSession innerList)
		{	
			if (entity == null)
				return false;
							
			#region Composite Parent Properties
			//Save Source Composite Properties, however, don't call deep save on them.  
			//So they only get saved a single level deep.
			
			#region SubCategoryIdSource
			if (CanDeepSave(entity, "ProcedureSubCategory|SubCategoryIdSource", deepSaveType, innerList) 
				&& entity.SubCategoryIdSource != null)
			{
				DataRepository.ProcedureSubCategoryProvider.Save(transactionManager, entity.SubCategoryIdSource);
				entity.SubCategoryId = entity.SubCategoryIdSource.SubCategoryId;
			}
			#endregion 
			#endregion Composite Parent Properties

			// Save Root Entity through Provider
			if (!entity.IsDeleted)
				this.Save(transactionManager, entity);
			
			//used to hold DeepSave method delegates and fire after all the local children have been saved.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();

			#region MemberTypeMemberTypeCollection_From_ProcedureDiscount>
			if (CanDeepSave(entity.MemberTypeMemberTypeCollection_From_ProcedureDiscount, "List<MemberType>|MemberTypeMemberTypeCollection_From_ProcedureDiscount", deepSaveType, innerList))
			{
				if (entity.MemberTypeMemberTypeCollection_From_ProcedureDiscount.Count > 0 || entity.MemberTypeMemberTypeCollection_From_ProcedureDiscount.DeletedItems.Count > 0)
				{
					DataRepository.MemberTypeProvider.Save(transactionManager, entity.MemberTypeMemberTypeCollection_From_ProcedureDiscount); 
					deepHandles.Add("MemberTypeMemberTypeCollection_From_ProcedureDiscount",
						new KeyValuePair<Delegate, object>((DeepSaveHandle<MemberType>) DataRepository.MemberTypeProvider.DeepSave,
						new object[] { transactionManager, entity.MemberTypeMemberTypeCollection_From_ProcedureDiscount, deepSaveType, childTypes, innerList }
					));
				}
			}
			#endregion 
	
			#region List<InvoiceDetails>
				if (CanDeepSave(entity.InvoiceDetailsCollection, "List<InvoiceDetails>|InvoiceDetailsCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(InvoiceDetails child in entity.InvoiceDetailsCollection)
					{
						if(child.ProcedureCodeSource != null)
						{
							child.ProcedureCode = child.ProcedureCodeSource.ProcedureCode;
						}
						else
						{
							child.ProcedureCode = entity.ProcedureCode;
						}

					}

					if (entity.InvoiceDetailsCollection.Count > 0 || entity.InvoiceDetailsCollection.DeletedItems.Count > 0)
					{
						//DataRepository.InvoiceDetailsProvider.Save(transactionManager, entity.InvoiceDetailsCollection);
						
						deepHandles.Add("InvoiceDetailsCollection",
						new KeyValuePair<Delegate, object>((DeepSaveHandle< InvoiceDetails >) DataRepository.InvoiceDetailsProvider.DeepSave,
							new object[] { transactionManager, entity.InvoiceDetailsCollection, deepSaveType, childTypes, innerList }
						));
					}
				} 
			#endregion 
				
	
			#region List<ProcedureDiscount>
				if (CanDeepSave(entity.ProcedureDiscountCollection, "List<ProcedureDiscount>|ProcedureDiscountCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(ProcedureDiscount child in entity.ProcedureDiscountCollection)
					{
						if(child.ProcedureCodeSource != null)
						{
								child.ProcedureCode = child.ProcedureCodeSource.ProcedureCode;
						}

						if(child.MemberTypeSource != null)
						{
								child.MemberType = child.MemberTypeSource.MemberType;
						}

					}

					if (entity.ProcedureDiscountCollection.Count > 0 || entity.ProcedureDiscountCollection.DeletedItems.Count > 0)
					{
						//DataRepository.ProcedureDiscountProvider.Save(transactionManager, entity.ProcedureDiscountCollection);
						
						deepHandles.Add("ProcedureDiscountCollection",
						new KeyValuePair<Delegate, object>((DeepSaveHandle< ProcedureDiscount >) DataRepository.ProcedureDiscountProvider.DeepSave,
							new object[] { transactionManager, entity.ProcedureDiscountCollection, deepSaveType, childTypes, innerList }
						));
					}
				} 
			#endregion 
				
			//Fire all DeepSave Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			
			// Save Root Entity through Provider, if not already saved in delete mode
			if (entity.IsDeleted)
				this.Save(transactionManager, entity);
				

			deepHandles = null;
						
			return true;
		}
		#endregion
	} // end class
	
	#region MedicalProcedureChildEntityTypes
	
	///<summary>
	/// Enumeration used to expose the different child entity types 
	/// for child properties in <c>VCS.Entities.MedicalProcedure</c>
	///</summary>
	public enum MedicalProcedureChildEntityTypes
	{
		
		///<summary>
		/// Composite Property for <c>ProcedureSubCategory</c> at SubCategoryIdSource
		///</summary>
		[ChildEntityType(typeof(ProcedureSubCategory))]
		ProcedureSubCategory,
	
		///<summary>
		/// Collection of <c>MedicalProcedure</c> as OneToMany for InvoiceDetailsCollection
		///</summary>
		[ChildEntityType(typeof(TList<InvoiceDetails>))]
		InvoiceDetailsCollection,

		///<summary>
		/// Collection of <c>MedicalProcedure</c> as OneToMany for ProcedureDiscountCollection
		///</summary>
		[ChildEntityType(typeof(TList<ProcedureDiscount>))]
		ProcedureDiscountCollection,

		///<summary>
		/// Collection of <c>MedicalProcedure</c> as ManyToMany for MemberTypeCollection_From_ProcedureDiscount
		///</summary>
		[ChildEntityType(typeof(TList<MemberType>))]
		MemberTypeMemberTypeCollection_From_ProcedureDiscount,
	}
	
	#endregion MedicalProcedureChildEntityTypes
	
	#region MedicalProcedureFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;MedicalProcedureColumn&gt;"/> class
	/// that is used exclusively with a <see cref="MedicalProcedure"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class MedicalProcedureFilterBuilder : SqlFilterBuilder<MedicalProcedureColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the MedicalProcedureFilterBuilder class.
		/// </summary>
		public MedicalProcedureFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the MedicalProcedureFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public MedicalProcedureFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the MedicalProcedureFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public MedicalProcedureFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion MedicalProcedureFilterBuilder
	
	#region MedicalProcedureParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;MedicalProcedureColumn&gt;"/> class
	/// that is used exclusively with a <see cref="MedicalProcedure"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class MedicalProcedureParameterBuilder : ParameterizedSqlFilterBuilder<MedicalProcedureColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the MedicalProcedureParameterBuilder class.
		/// </summary>
		public MedicalProcedureParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the MedicalProcedureParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public MedicalProcedureParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the MedicalProcedureParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public MedicalProcedureParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion MedicalProcedureParameterBuilder
	
	#region MedicalProcedureSortBuilder
    
    /// <summary>
    /// A strongly-typed instance of the <see cref="SqlSortBuilder&lt;MedicalProcedureColumn&gt;"/> class
	/// that is used exclusively with a <see cref="MedicalProcedure"/> object.
    /// </summary>
    [CLSCompliant(true)]
    public class MedicalProcedureSortBuilder : SqlSortBuilder<MedicalProcedureColumn>
    {
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the MedicalProcedureSqlSortBuilder class.
		/// </summary>
		public MedicalProcedureSortBuilder() : base() { }

		#endregion Constructors

    }    
    #endregion MedicalProcedureSortBuilder
	
} // end namespace
